//
//  main.m
//  04_NSDictionaryHomework
//
//  Created by 徐昊 on 16/7/7.
//  Copyright © 2016年 徐昊. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Color.h"
int main(int argc, const char * argv[]) {
//    需要的处理文件的下载地址 处理⽂件crayons.txt中的⽂本信息，⽂本内容是关于颜⾊的，每⾏都是⼀个颜⾊的信息，例如：Almond #EED9C4，前⼀个字符串是颜⾊的名称，后⼀个字符串是颜⾊的16进制⾊值，处理⽂本完成如下需求:
//    
//    使⽤字典管理所有的颜⾊，即字典中存储的是多个键值对，颜⾊名称为 key，16进制颜⾊值（不带#）是value。
//    使⽤⼀个新的字典管理颜⾊，对颜⾊进⾏分类管理，即：“A”，“B”，“C”… 即这个字典包含多个键值对，key是26个字⺟，value是数组，数组⾥存放的是 Color对象（包含name和colorValue）。需要⾃⼰创建Color类。
//    获取文件中字符串的方法如下:
//    
//    NSString *path = [[NSBundle mainBundle] pathForResource:@"crayons" ofType:@"txt"];
//    NSString *sourceStr = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
//    根据得到的sourceStr进行处理
    // 1.
#if 0
    NSString * dataString = [NSString stringWithContentsOfFile:@"/Users/xuhao/DLS160710/Objective-C/HomeWork/04_NSDictionaryHomework/04_NSDictionaryHomework/crayons.txt" encoding:NSUTF8StringEncoding error:nil];
//    NSLog(@"%@", dataString);
    NSArray *colorArr = [dataString componentsSeparatedByString:@"\n"];
    NSMutableDictionary *colorDic = [[NSMutableDictionary alloc] init];
    for (NSString *string in colorArr) {
        NSArray *arr = [string componentsSeparatedByString:@" #"];
        [colorDic setObject:arr[1] forKey:arr[0]];
    }
    NSLog(@"%@", colorDic);
    
#endif
    // 2.
    NSString * dataString = [NSString stringWithContentsOfFile:@"/Users/xuhao/DLS160710/Objective-C/HomeWork/04_NSDictionaryHomework/04_NSDictionaryHomework/crayons.txt" encoding:NSUTF8StringEncoding error:nil];
    //    NSLog(@"%@", dataString);
    NSArray *colorArr = [dataString componentsSeparatedByString:@"\n"];
    NSMutableArray *colorArray = [[NSMutableArray alloc] init];
    for (NSString *string in colorArr) {
        NSArray *arr = [string componentsSeparatedByString:@" #"];
        Color *color = [[Color alloc] initWithName:arr[0] colorValue:arr[1]];
        [colorArray addObject:color];
    }
//    NSLog(@"%@", colorArray);
    NSMutableDictionary *colorDic = [[NSMutableDictionary alloc] init];
    for (Color *color in colorArray) {
        unichar capital = [[color.name capitalizedString] characterAtIndex:0];
        NSString *group = [NSString stringWithFormat:@"%c", capital];
        NSMutableArray *colorCapitalArray = [colorDic objectForKey:group];
        if (colorCapitalArray == nil) {
            colorCapitalArray = [NSMutableArray arrayWithObject:color];
            [colorDic setObject:colorCapitalArray forKey:group];
        } else {
            [colorCapitalArray addObject:color];
        }
    }
    NSLog(@"%@", colorDic);

    
    
    
    
    return 0;
}
